package com.wu.advapplication.utils;
/**
 * 设置自启权限
 */

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import com.wu.advapplication.R;

public class SelfStartUtil {
    private static final String TAG = "SelfStartUtil";

    public static void start() {


        if (Build.MANUFACTURER.equals("Xiaomi")) {

        } else if (Build.MANUFACTURER.equals("samsung")) {

        } else if (Build.MANUFACTURER.equals("HUAWEI")) {

        } else if (Build.MANUFACTURER.equals("vivo")) {

        } else if (Build.MANUFACTURER.equals("Meizu")) {

        } else if (Build.MANUFACTURER.equals("OPPO")) {

        } else if (Build.MANUFACTURER.equals("oneplus")) {

        } else if (Build.MANUFACTURER.equals("letv")) {

        } else if (Build.MANUFACTURER.equals("zte")) {

        } else if (Build.MANUFACTURER.equals("F")) {
            //金立
        } else if (Build.MANUFACTURER.equals("smartisanos")) {

        } else if (Build.MANUFACTURER.equals("360")) {

        } else if (Build.MANUFACTURER.equals("coolpad")) {

        }
    }

    /**
     * 小米
     *
     * @param context
     */
    public static void startByMiaoMi(Context context) {
        try {
            // MIUI 8
            Log.e(TAG, "startByMiaoMi: MIUI 8");
            Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
            localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            localIntent.putExtra("extra_pkgname", context.getPackageName());
            context.startActivity(localIntent);
        } catch (Exception e) {
            try {
                // MIUI 5/6/7
                Log.e(TAG, "startByMiaoMi: MIUI 5/6/7");
                Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
                localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
                localIntent.putExtra("extra_pkgname", context.getPackageName());
                context.startActivity(localIntent);
            } catch (Exception e1) {
                // 否则跳转到应用详情
                startToAppInfo(context);
            }
        }
    }

    /**
     * 华为
     *
     * @param context
     */
    public static void startByHuaWei(Context context) {
        ComponentName componentName = null;
        int sdkVersion = Build.VERSION.SDK_INT;
        try {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //跳自启动管理
            if (sdkVersion >= 28) {//9:已测试
                componentName = ComponentName.unflattenFromString("com.huawei.systemmanager/.startupmgr.ui.StartupNormalAppListActivity");//跳自启动管理
            } else if (sdkVersion >= 26) {//8：已测试
                componentName = ComponentName.unflattenFromString("com.huawei.systemmanager/.appcontrol.activity.StartupAppControlActivity");
            } else if (sdkVersion >= 23) {//7.6：已测试
                componentName = ComponentName.unflattenFromString("com.huawei.systemmanager/.startupmgr.ui.StartupNormalAppListActivity");
            } else if (sdkVersion >= 21) {//5
                componentName = ComponentName.unflattenFromString("com.huawei.systemmanager/com.huawei.permissionmanager.ui.MainActivity");
            }
            //componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");//锁屏清理
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            //跳转失败
            startToAppInfo(context);
        }
    }

    /**
     * 三星
     *
     * @param context
     */
    public static void startBySamsung(Context context) {
        Intent intent = new Intent();
        ComponentName componentName = null;
        try {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //跳自启动管理
            componentName = ComponentName.unflattenFromString("com.samsung.android.sm/.app.dashboard.SmartManagerDashBoardActivity");
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            try {
                componentName = new ComponentName("com.samsung.android.sm_cn", "com.samsung.android.sm.ui.ram.AutoRunActivity");
                intent.setComponent(componentName);
                context.startActivity(intent);
            } catch (Exception e1) {
                //跳转失败
                startToAppInfo(context);
            }
        }
    }

    /**
     * Meizu
     *
     * @param context
     */
    public static void startByMeizu(Context context) {
        Intent intent = new Intent();
        ComponentName componentName = null;
        try {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //跳自启动管理
            componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.SmartBGActivity");//跳转到后台管理页面
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            try {
                componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.PermissionMainActivity");//跳转到手机管家
                intent.setComponent(componentName);
                context.startActivity(intent);
            } catch (Exception e1) {
                //跳转失败
                startToAppInfo(context);
            }
        }
    }

    /**
     * vivo
     *
     * @param context
     */
    public static void startByVivo(Context context) {
        Intent localIntent;
        try {
            if (((Build.MODEL.contains("Y85")) && (!Build.MODEL.contains("Y85A"))) || (Build.MODEL.contains("vivo Y53L"))) {
                localIntent = new Intent();
                localIntent.setClassName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.PurviewTabActivity");
                localIntent.putExtra("packagename", context.getPackageName());
                localIntent.putExtra("tabId", "1");
                context.startActivity(localIntent);
            } else {
                localIntent = new Intent();
                localIntent.setClassName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.SoftPermissionDetailActivity");
                localIntent.setAction("secure.intent.action.softPermissionDetail");
                localIntent.putExtra("packagename", context.getPackageName());
                context.startActivity(localIntent);
            }
        } catch (Exception e) {
            // 否则跳转到应用详情
            startToAppInfo(context);
        }
    }

    /**
     * OPPO
     *
     * @param context
     */
    public static void startByOPPO(Context context) {
        Intent intent = new Intent();
        try {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("packageName", context.getPackageName());
            ComponentName comp = new ComponentName("com.color.safecenter", "com.color.safecenter.permission.PermissionManagerActivity");
            intent.setComponent(comp);
            context.startActivity(intent);
        } catch (Exception e) {
            try {
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("pkg_name", context.getPackageName());
                intent.putExtra("app_name", context.getString(R.string.app_name));
                intent.putExtra("class_name", "com.welab.notificationdemo.MainActivity");
                ComponentName comp = new ComponentName("com.coloros.notificationmanager", "com.coloros" +
                        ".notificationmanager.AppDetailPreferenceActivity");
                intent.setComponent(comp);
                context.startActivity(intent);
            } catch (Exception e1) {
                // 否则跳转到应用详情
                startToAppInfo(context);
            }
        }
    }

    /**
     * 应用详情
     *
     * @param context
     */
    public static void startToAppInfo(Context context) {
        Log.e(TAG, "startToAppInfo");
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 9) {
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            intent.setAction(Intent.ACTION_VIEW);
            intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        context.startActivity(intent);
    }
}
